/*
* 题目： 编程实现控制台版并支持两人对战的五子棋游戏。

（1）绘制棋盘 - 写一个成员方法实现

（2）提示黑方和白方分别下棋并重新绘制棋盘 - 写一个成员方法实现。

（3）每当一方下棋后判断是否获胜 - 写一个成员方法实现。
* */

import java.util.Scanner;

public class WuZiQi {

    String [][]qiPan = new String[17][17];
    //判断赢家
    int win = 0;
    //1.定义一个方法，绘制棋盘
    void Draw(){
        //1.1 创建一个一维String数组用于存储棋盘上标,大小为16
        //ASCII码：0-9对应48-57，a-f对应97-102
        char []index = new char[16];
        for(int i = 0; i < 10; i++){
            index[i] = (char)(48+i);
        }
        for(int i = 10; i < 16; i++){
            index[i] = (char)(87+i);
        }
        //1.2 给棋盘各个位置赋值
        for(int i = 0; i < 17; i++){
            for(int j = 0; j < 17; j++){
                if(i == 0 && j == 0){
                    qiPan[i][j] = "   ";
                }
                else if(i == 0 && j != 0){
                    qiPan[i][j] = index[j-1] + "  ";
                }
                else if(i != 0 && j == 0){
                    qiPan[i][j] = index[i-1] + "  ";
                }
                else{
                    qiPan[i][j] = "+  ";
                }
            }
        }
    }

    //2.提示黑方和白方分别下棋并重新绘制棋盘 黑方以●表示，白方以○表示,黑方白方下棋方式均以在控制台输入棋盘对应的坐标进行
    String [][] RunBlack(int []black){
        qiPan[black[0] + 1][black[1] + 1] = "●  ";
        return qiPan;
    }
    String [][] RunWhite(int []white){
        qiPan[white[0] + 1][white[1] + 1] = "○  ";
        return qiPan;
    }

    //打印棋盘
    void PrintBoard(String [][]board){
        for(int i = 0; i < 17; i++){
            for(int j =0; j < 17; j++){
                System.out.print(qiPan[i][j]);
            }
            System.out.println();
        }
    }

    //4.判断比赛是否结束以及胜利者
    boolean Result(String [][]board){
        for (int i = 1; i < 17; i++) {
            for (int j = 1; j < 17; j++) {
                //判断黑子是否达成胜利条件
                if (board[i][j].equals("●  ")){
                    //向右
                    if(j < 13){
                        if(board[i][j].equals(board[i][j+1])
                            && board[i][j].equals(board[i][j+2])
                            && board[i][j].equals(board[i][j+3])
                            && board[i][j].equals(board[i][j+4])){
                            win = 1;
                            return true;
                        }
                    }
                    //向下
                    if(i < 13){
                        if(board[i][j].equals(board[i+1][j])
                                && board[i][j].equals(board[i+2][j])
                                && board[i][j].equals(board[i+3][j])
                                && board[i][j].equals(board[i+4][j])){
                            win = 1;
                            return true;
                        }
                    }
                    //向右下
                    if(i < 13 && j < 13){
                        if(board[i][j].equals(board[i+1][j+1])
                                && board[i][j].equals(board[i+2][j+2])
                                && board[i][j].equals(board[i+3][j+3])
                                && board[i][j].equals(board[i+4][j+4])){
                            win = 1;
                            return true;
                        }
                    }
                    //向右上
                    if(i > 4 && j < 13){
                        if(board[i][j].equals(board[i-1][j+1])
                                && board[i][j].equals(board[i-2][j+2])
                                && board[i][j].equals(board[i-3][j+3])
                                && board[i][j].equals(board[i-4][j+4])){
                            win = 1;
                            return true;
                        }
                    }
                }
                //判断白子是否达成胜利条件
                if (board[i][j].equals("○  ")){
                    //向右
                    if(j < 13){
                        if(board[i][j].equals(board[i][j+1])
                                && board[i][j].equals(board[i][j+2])
                                && board[i][j].equals(board[i][j+3])
                                && board[i][j].equals(board[i][j+4])){
                            win = 2;
                            return true;
                        }
                    }
                    //向下
                    if(i < 13){
                        if(board[i][j].equals(board[i+1][j])
                                && board[i][j].equals(board[i+2][j])
                                && board[i][j].equals(board[i+3][j])
                                && board[i][j].equals(board[i+4][j])){
                            win = 2;
                            return true;
                        }
                    }
                    //向右下
                    if(i < 13 && j < 13){
                        if(board[i][j].equals(board[i+1][j+1])
                                && board[i][j].equals(board[i+2][j+2])
                                && board[i][j].equals(board[i+3][j+3])
                                && board[i][j].equals(board[i+4][j+4])){
                            win = 2;
                            return true;
                        }
                    }
                    //向右上
                    if(i > 4 && j < 13){
                        if(board[i][j].equals(board[i-1][j+1])
                                && board[i][j].equals(board[i-2][j+2])
                                && board[i][j].equals(board[i-3][j+3])
                                && board[i][j].equals(board[i-4][j+4])){
                            win = 2;
                            return true;
                        }
                    }
                }
            }
        }

        return false;
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        //创建黑白子坐标数组
        int [] black = new int[2];
        int [] white = new int[2];

        //创建新棋盘
        String [][] board = new String[17][17];

        //3.调用方法绘制默认棋盘
        WuZiQi game = new WuZiQi();
        //将默认棋盘赋给游戏棋盘
        board = game.qiPan;
        game.Draw();
        game.PrintBoard(board);
        System.out.println("请黑方白方分别下棋");
        System.out.println("黑方以●表示，白方以○表示");



        //判断游戏是否结束，默认值为false
        boolean flag = false;

        outer:while(!flag){

            System.out.println("黑方先手，请输入棋子坐标：");
            for (int i = 0; i < 2; i++) {
                String str = sc.next();
                black[i] = Integer.parseInt(str,16);
            }
            //判断该下标是否有棋子
            while(!board[black[0]+1][black[1]+1].equals("+  ") ){
                System.out.println("该位置已有棋子");
                System.out.println("黑方先手，请输入棋子坐标：");
                for (int i = 0; i < 2; i++) {
                    String str = sc.next();
                    black[i] = Integer.parseInt(str,16);
                }
            }
            //在棋盘上下棋,并打印棋盘
            board = game.RunBlack(black);
            game.PrintBoard(board);

            if(game.win != 0){
                flag = game.Result(board);
                break outer;
            }
            System.out.println("白方后手，请输入棋子坐标：");
            for (int i = 0; i < 2; i++) {
                String st = sc.next();
                white[i] = Integer.parseInt(st,16);
            }
            //判断该下标是否有棋子
            while(!board[white[0]+1][white[1]+1].equals("+  ")){
                System.out.println("该位置已有棋子");
                System.out.println("白方后手，请输入棋子坐标：");
                for (int i = 0; i < 2; i++) {
                    String st = sc.next();
                    white[i] = Integer.parseInt(st,16);
                }
            }
            //在棋盘上下棋,并打印棋盘
            board = game.RunWhite(white);
            game.PrintBoard(board);


            //判断游戏是否产生胜利者
            flag = game.Result(board);

        }
        if(game.win == 1){
            System.out.println("黑方获胜");
        }
        else if(game.win == 2){
            System.out.println("白方获胜");
        }
    }
}
